const std = @import("std");


pub const Blocks = struct {
	init_width: u8,
	init_height: u8,
	allocator: std.mem.Allocator = std.heap.page_allocator,
	_blocks: std.AutoHashMap(u16, Block) = undefined,


	pub fn init(self: *Blocks) !void {
		self._blocks = std.AutoHashMap(u16, Block).init(self.allocator);

		for (0..self.init_height) |y_| {
			const y: u8 = @intCast(y_);
			for (0..self.init_width) |x_| {
				const x: u8 = @intCast(x_);

				const key: u16 = self.calc_key(x, y);
				const value = Block{.x = x, .y = y};
				try self._blocks.put(key, value);
			}
		}
	}


	pub fn deinit(self: *Blocks) void {
		self._blocks.deinit();
	}


	pub fn get_blocks(self: *Blocks) std.AutoHashMap(u16, Block).Iterator {
		return self._blocks.iterator();
	}


	pub fn empty(self: *Blocks) bool {
		return self._blocks.count() == 0;
	}


	pub fn colliding(self: *Blocks, x: u8, y: u8) bool {
		const key: u16 = self.calc_key(x, y);
		return self._blocks.contains(key);
	}


	pub fn remove_block(self: *Blocks, x: u8, y: u8) bool {
		const key: u16 = self.calc_key(x, y);
		return self._blocks.remove(key);
	}


	fn calc_key(self: *Blocks, x: u8, y: u8) u16 {
		return x + @as(u16, y) * self.init_width;
	}
};


pub const Block = struct {
	x: u8,
	y: u8,
};
